<template>
  <el-dialog v-if="load" :close-on-click-modal="false" title="生成对账单" v-model="visible" align-center width="1300px" :z-index="2000"
    @close="handleClose">
    <el-form ref="formRef" :model="form" label-width="110px" label-position="top">
      <div class="v-title mb-3">基础信息</div>
      <div class="grid grid-cols-4 gap-x-3">
        <el-form-item label="供应商">
          <el-input v-model="form.basic.supplier_name" placeholder="请输入" disabled />
        </el-form-item>
        <el-form-item label="供应商类型">
          <VSelect v-model="form.basic.purchase_type" placeholder="请选择" :options="options.purchase_type || []" filterable
            clearable disabled />
        </el-form-item>
        <el-form-item label="结算方式">
          <el-input v-model="form.basic.period_name" placeholder="请输入" disabled />
        </el-form-item>
        <el-form-item label="公司主体">
          <VSelect v-model="form.basic.company_id" placeholder="请选择" :options="options.company_id || []" filterable
            clearable disabled class="w-full" />
        </el-form-item>
        <el-form-item label="交易方式">
          <VSelect v-model="form.basic.payment_type" placeholder="请选择" :options="options.payment_type || []" filterable
            clearable class="w-full" />
        </el-form-item>
        <!-- <el-form-item label="支付账号">
          <VSelect v-model="form.basic.payment_account" placeholder="请选择" :options="options.alipay_account || []"
            filterable clearable  />
        </el-form-item> -->
        <el-form-item label="支付币种">
          <VSelect v-model="form.basic.currency" placeholder="请选择" :options="options.currency || []" filterable clearable
            class="w-full" />
        </el-form-item>
        <el-form-item label="收款人">
          <el-input v-model="form.basic.collect_name" placeholder="请输入" class="w-full" />
        </el-form-item>
        <el-form-item label="收款账号">
          <el-input v-model="form.basic.collect_account" placeholder="请输入" class="w-full" />
        </el-form-item>
        <el-form-item label="银行名称">
          <el-input v-model="form.basic.collect_bank" placeholder="请输入" disabled />
        </el-form-item>
        <el-form-item label="支行">
          <el-input v-model="form.basic.bank_address" placeholder="请输入" disabled />
        </el-form-item>
        <el-form-item label="备注" class="col-span-full">
          <el-input v-model="form.basic.remark" placeholder="请输入" type="textarea"
            :autosize="{ minRows: 2, maxRows: 5 }" />
        </el-form-item>
      </div>
      <div class="v-title mb-3">采购单信息</div>
      <vxe-table ref="tableRef" :data="form.purchaseList" size="small">
        <vxe-column title="采购单号" :min-width="140">
          <template #default="{ row }">
            <VText :value="row.purchase_no" type="link" />
          </template>
        </vxe-column>
        <vxe-column title="采购单号" :min-width="140">
          <template #header>下单时间<br />合同协议号</template>
          <template #default="{ row }">
            <div>{{ row.platform_order_time }}</div>
            <VText :value="row.contract_no" type="link" />
          </template>
        </vxe-column>
        <vxe-column title="sku" field="sku" :min-width="100">
          <template #default="{ row }">
            <VText :value="row.sku" type="link" />
          </template>
        </vxe-column>
        <vxe-column title="产品名称" field="sku_cn_title" :min-width="100">
          <template #default="{ row }">
            <VText :value="row.sku_cn_title" line="2" />
          </template>
        </vxe-column>
        <vxe-column title="下单数量" field="purchase_num" :min-width="80" />
        <vxe-column title="含税单价" field="unit_price_tax" :min-width="80" />
        <vxe-column title="含税总金额" field="total_price_tax" :min-width="80" />
        <vxe-column title="不含税单价" field="unit_purchase_price" :min-width="80" />
        <vxe-column title="不含税总金额" field="total_price" :min-width="80" />
        <vxe-column title="入库数量" field="num" :min-width="80" />
        <vxe-column title="入库时间" field="create_time" :min-width="160" />
        <vxe-column title="入库仓" field="inbound_warehouse" :min-width="80" />
        <vxe-column title="备注" field="remark" :min-width="80">
          <template #default="{ row }">
            <VText :value="row.remark" line="2" />
          </template>
        </vxe-column>
      </vxe-table>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.submit" @click="create()">提 交</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import api from '../api'
import { ElMessage } from 'element-plus'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
let loadings = reactive({})

// 表单处理
const formRef = ref()
const form = ref({})

// 打开弹窗
const open = async (selection) => {
  if (!load.value) {
    load.value = true
    await 1
  }
  if (!selection.length) {
    ElMessage.warning('请先选择行')
    return
  }
  form.value = {
    basic: {},
    purchaseList: [],
  }
  loadings = reactive({})
  formRef?.value?.resetFields()
  visible.value = true

  const res = await api.detail({ sb_ids: selection.map(d => d.sb_id) })
  form.value = res.data
  form.value.basic.return_type = 1
  form.value.purchaseList.forEach(d => {
    d.apply_refund_price = d.can_refund_price
  })
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const create = async () => {
  const validator = await formRef?.value.validate()
  if (!validator) return
  loadings.submit = true
  api.add(form.value).then(res => {
    ElMessage.success('操作成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.submit = false
  })
}

defineExpose({ open })
</script>